package com.fenger.billbar.demo;

/*
 *
 * Copyright (c) 1996-2001 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.ColorInterpolator;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Texture;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;

/**
 * Color Interpolator example
 * 
 * Very similar to texture mapping example, will interpolate the color values of
 * the earth. </br>
 * <strong>颜色插入器</strong></br>
 * 添加了代码以将颜色插入器附加到旋转的地球上。它不影响 TransformGroup，而是影响 Material
 * 对象中的散射颜色。因为在渲染循环开始之后，Material 对象将会变化，所以我设置了其能力位。
 */

public class ColorInterp extends Applet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SimpleUniverse universe;
	private BranchGroup scene;
	private Canvas3D canvas;
	private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);

	public Primitive createGeometry(int filter, java.net.URL texImage, Appearance appearance) {
		/**
		 * Create Sphere and texture it
		 */

		TextureLoader tex = new TextureLoader(texImage, TextureLoader.GENERATE_MIPMAP, this);
		Texture texture = tex.getTexture();
		texture.setMinFilter(filter);
		appearance.setTexture(texture);

		TextureAttributes texAttr = new TextureAttributes();
		texAttr.setTextureMode(TextureAttributes.MODULATE);
		appearance.setTextureAttributes(texAttr);

		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f gray = new Color3f(0.3f, 0.3f, 0.3f);
		Color3f ltgray = new Color3f(0.6f, 0.6f, 0.6f);

		// Set up the material properties
		appearance.setMaterial(new Material(white, black, ltgray, ltgray, 32.0f));

		Sphere sphere = new Sphere(.4f, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS, 100, appearance);

		return sphere;
	}

	public void setupView() {
		/**
		 * Add some view related things to view branch side of scene graph
		 */
		// add mouse interaction to the ViewingPlatform
		OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
		orbit.setSchedulingBounds(bounds);

		ViewingPlatform viewingPlatform = universe.getViewingPlatform();
		// This will move the ViewPlatform back a bit so the
		// objects in the scene can be viewed.
		viewingPlatform.setNominalViewingTransform();
		viewingPlatform.setViewPlatformBehavior(orbit);

	}

	public BranchGroup createSceneGraph() {
		// Create the root of the branch graph
		BranchGroup objRoot = new BranchGroup();

		// Create a simple Shape3D node; add it to the scene graph.
		// Set up the texture map
		java.net.URL texImage = null;
		// the path to the image
		try {
			// texImage = new
			// java.net.URL("file:D:/new_code_workspace/my-project-myjavatest/src/res/images/earth.jpeg");
			texImage = new java.net.URL("file:D:/new_code_workspace/my-project-myjavatest/src/res/ball-hdpi/ball2.png");
		} catch (java.net.MalformedURLException ex) {
			System.out.println(ex.getMessage());
			System.exit(1);
		}

		Appearance app = new Appearance();
		Primitive geo = createGeometry(Texture.MULTI_LEVEL_LINEAR, texImage, app);

		// spinGroup will be hooked into the interpolator
		TransformGroup spinGroup = new TransformGroup();
		spinGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		spinGroup.addChild(geo);

		// Create a new Behavior object that will perform the
		// desired operation on the specified transform and add
		// it into the scene graph.
		Alpha rotationAlpha = new Alpha(-1, 4000);

		RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spinGroup);
		rotator.setSchedulingBounds(bounds);

		// we'll need the Material to Interpolate the diffuse color
		// set capability bit to allow interpolator to change at render time
		Material mat = app.getMaterial();
		mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
		Alpha colorAlpha = new Alpha(-1, 2000);

		// We interpolate from black to white, looping indefinitely
		Color3f endColor = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f startColor = new Color3f(0.0f, 0.0f, 0.0f);
		ColorInterpolator colorInterp = new ColorInterpolator(colorAlpha, mat, startColor, endColor);
		colorInterp.setSchedulingBounds(bounds);

		// throw in some light so we aren't stumbling
		// around in the dark
		Color3f lightColor = new Color3f(.5f, .5f, .5f);
		AmbientLight ambientLight = new AmbientLight(lightColor);
		ambientLight.setInfluencingBounds(bounds);
		DirectionalLight directionalLight = new DirectionalLight();
		directionalLight.setColor(lightColor);
		directionalLight.setInfluencingBounds(bounds);

		objRoot.addChild(rotator); // behavior gets attached at the top
		objRoot.addChild(colorInterp); // behavior gets attached at the top
		objRoot.addChild(spinGroup); // TransformGroup and sphere
		objRoot.addChild(directionalLight);
		objRoot.addChild(ambientLight);

		return objRoot;

	}

	public ColorInterp() {
	}

	public void init() {
		BranchGroup scene = createSceneGraph();

		setLayout(new BorderLayout());
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		canvas = new Canvas3D(config);
		add("Center", canvas);

		// Create a simple scene and attach it to the virtual universe
		universe = new SimpleUniverse(canvas);
		setupView();

		universe.addBranchGraph(scene);
	}

	public void destroy() {
		universe.removeAllLocales();
	}

	//
	// The following allows ColorInterp to be run as an application
	// as well as an applet
	//
	public static void main(String[] args) {
		new MainFrame(new ColorInterp(), 256, 256);
	}
}
